package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import model.StudentDBModel;
import data.StudentData;

import java.util.Vector;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class AddressAction extends Action {
	
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		
		StudentData studentData = (StudentData) request.getSession().getAttribute("studentData");
		
		if(request.getParameter("street") != null)
		{
			studentData.setStreet(request.getParameter("street"));
			studentData.setCity(request.getParameter("city"));
			if(studentData.isUsResident())
			{
				studentData.setState(request.getParameter("state"));
			}
			else
			{
				studentData.setCountryCode(request.getParameter("countryCode"));
			}
			studentData.setZip(request.getParameter("zip"));
			studentData.setAreaCode(request.getParameter("areaCode"));
			studentData.setPhone(request.getParameter("phone"));
		}
		
		Vector<String> formData = studentData.getFormData();
		
		request.getSession().setAttribute("formData", formData);
		request.getSession().setAttribute("studentData", studentData);
		
		if(studentData.getCitizen().equals("United States"))
		{
			RowSet crsCountries = StudentDBModel.getAllCountries();
			request.setAttribute("crsCountries", crsCountries);
			
			RowSet crsStates = StudentDBModel.getAllStates();
			request.setAttribute("crsStates", crsStates);
			return mapping.findForward("usResident");
		}
		else
		{
			return mapping.findForward("nonResident");
		}
	}

}
